package com.prashantdctm.paidemail;

import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.time.StopWatch;
import org.apache.log4j.Logger;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.RefreshHandler;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HTMLParserListener;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.prashantdctm.utils.EmailUtil;
import com.prashantdctm.utils.GeneralUtil;
import com.prashantdctm.utils.HttpUtil;


public class CheckURLServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	final static boolean PROXY_ENABLED = false;
	private static WebClient webClient = null;
	private static final Logger log = Logger.getLogger(CheckURLServlet.class.getName());
	
	public static void main(String[] args) throws Exception {
		initWebClient();
		String url = "http://www.gsdsdsdasd.com";
		try {
			System.out.println("Connecting to URL: "+url);
			HtmlPage page = webClient.getPage(url);
			System.out.println(page.asXml());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		initWebClient();
		
		try {
			System.out.println("Connecting to URL: "+req.getParameter("url"));
			HtmlPage page = webClient.getPage(req.getParameter("url"));
			resp.getWriter().println(page.asXml());
		} catch (Exception e) {
			customLog("Exception in getting url"+e.getMessage());
			resp.getWriter().println("Exception in getting url"+e.getMessage());
			e.printStackTrace();
		}
	}
	
    public static void initWebClient(){
    	customLog("Initializing webClient");
    	try {
    		if (PROXY_ENABLED) {
    			webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_8,"10.113.26.80", 3128);
				// set proxy username and password
				final DefaultCredentialsProvider credentialsProvider = (DefaultCredentialsProvider) webClient.getCredentialsProvider();
				credentialsProvider.addProxyCredentials("prashantkumar", "Qandi!@90");
			} else {
				webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_6);
			}

			webClient.setRefreshHandler(new RefreshHandler() {
				public void handleRefresh(Page page, URL url, int arg)
						throws IOException {
					
				}
			});
			// webClient.setProxyConfig(pc);
			webClient.setHTMLParserListener(HTMLParserListener.LOG_REPORTER);
			webClient.setPopupBlockerEnabled(false);
			webClient.setTimeout(50000); // timout in ms
			webClient.setRedirectEnabled(true); // follow redirects
			webClient.setThrowExceptionOnFailingStatusCode(false);
			webClient.setJavaScriptEnabled(true);
			webClient.setThrowExceptionOnScriptError(false);
			webClient.setUseInsecureSSL(true);
			webClient.setCssEnabled(true);
    	} catch (Exception e) {
    		System.out.println("error initializing webclient)");
    		e.printStackTrace();
    	}
    }
    
    public static void customLog(String msg) {
    	log.info(msg);
    }
}

